package com.huayi.gg.activity;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.media.session.MediaController;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.huayi.gg.R;
import com.huayi.gg.ui.CustomVideoView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class VideoActivity extends Activity {


    CustomVideoView mVideoView;
    MediaController mMediaController;
    private Context mcontent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_video);
        mcontent = this;
        mVideoView = findViewById(R.id.video_view);



        String file_path=getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()+"/video";
        Deposit(file_path,"test.mp4");

        //使用VideoView播放视频
        String path=file_path+"/wanjia.mp4";
        Log.d("播放地址",path);



        mVideoView.setVideoPath(path);
        mVideoView.start();



    }
    //将assets内文件存储到本地下载目录
    public Boolean Deposit(String path,String fileName){
        InputStream inputStream;
        try {

            //判断文件是否存在
            File file1=new File(path + "/" + fileName);

            if(!file1.exists()){

                inputStream=getAssets().open(fileName);
                File file = new File(path);
                //当目录不存在时创建目录
                if(!file.exists()){
                    file.mkdirs();
                }

                FileOutputStream fileOutputStream = new FileOutputStream(path + "/" + fileName);// 保存到本地的文件夹下的文件
                byte[] buffer = new byte[1024];
                int count = 0;
                while ((count = inputStream.read(buffer)) > 0) {
                    fileOutputStream.write(buffer, 0, count);
                }
                fileOutputStream.flush();
                fileOutputStream.close();
                inputStream.close();

            }else {
                Toast.makeText(VideoActivity.this,"已存在", Toast.LENGTH_LONG).show();

            }

            return true;


        }catch (IOException e){
            e.printStackTrace();
        }
        return false;
    }


}